#!/bin/bash
#  Name: distcmd.sh
#  Author:  Marco Gomiero
#  Date:    Wed Aug 28
#  Version: 0.0.1
#  Description: execute a shell command in a distributed way on all host specified in ./distcmd.hosts

i=0
while read line; do
        HOSTLIST[$i]="$line"
        i=$(($i+1))
done < ./distcmd.hosts

if [ $# -eq 0 ] ; then
   echo "a shell command is required."
   exit 0
fi

for host in $HOSTLIST; do
   ESCAPEDCMD=$(echo "$@" | sed "s/|/\\\|/g")
   echo "Sending command '$ESCAPEDCMD' to $host:"
   ssh -t $host $@
   RC=$?
   if [ $RC -gt 0 ] ; then
      echo "RC=$RC"
   fi
   echo "----------------------------------------"
done
